<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/4/30
 * Time: 10:51
 */

namespace app\index\model;


use think\Model;
use app\index\model\CrmManage;
class CrmFenzu extends Model
{

    /*
     * 销售
     * 级别
     * */
    public function authority($zu_id,$is_xiaoshou,$username){
        if($is_xiaoshou!=1)return '非销售';
        if(!$fenzu = self::get($zu_id))return [];
        $ids = $this->fenzu($fenzu['id']);
        if(count($ids)!=0){
            array_unshift($ids,$fenzu['id']);
            $username_arr = CrmManage::where('zu_id','in',$ids)->column('username');
        }else{
            $username_arr = [$username];
        }
        return $username_arr;
    }

    /*
     * 财务
     * */
    public function finance($zu_id_arr = []){
        $authority=[];
        foreach ($zu_id_arr as $value){
           if($zu = $this->get($value)){
               if($zu['pid']==0){
                   array_unshift($authority,$value);
                   $authority = array_merge($authority,$this->fenzu($value));
               }
           }
        }
       return $authority;
    }

    public function zu_username($zu_id){
        $arr = $this->fenzu($zu_id);
        array_push($arr,$zu_id);
        return  CrmManage::where('zu_id','in',$arr)->column('username');
    }

    /*
     * 递归
     * */
    public function fenzu($id){
        $ids = [];
        $zu = self::where('id',$id)->find();
        if($zu['pid']==0){
            return $this->digui($id,$ids);
        }else{
            return  $this->digui($zu['id'],$ids);
        }
    }

    /*
     * 递归
     * */
    protected function digui($id,&$ids){
        $res = self::where('pid',$id)->select();
        if(count($res)>=1){
            foreach ($res as $value){
                array_unshift($ids,$value['id']);
                $this->digui($value['id'],$ids);
            }
        }
        return $ids;
    }



}